var receivingId=getUrlVal("receivingId");
var pageTips=getUrlVal("pageTips");
var obj={};
var userMess="";
$(function(){
    //获取用户信息
    getUser();
    userMess=JSON.stringify(ym.user);
    //接种所在地
    var area2 = new LArea();
    var provs_data=[];
    $.ajax({
        url: "/web/areaController/queryTree",
        success: function (data) {
            if(data.obj){
                provs_data=data.obj;
            }
            area2.init({
                'trigger': '#address',
                'valueTo': '#value2',
                'keys': {
                    id: 'id',
                    name: 'name'
                },
                'type': 1,
                'data':provs_data
            });
        }
    })
    var address=document.querySelector('#address');
    address.addEventListener('blur', function () {
        var provinceCode=localStorage.getItem('provinceCode');
        var cityCode=localStorage.getItem('cityCode');
        var countyCode=localStorage.getItem('countyCode');
        var addr=address.value;
        $("#value2").attr("provinceCode",provinceCode)
        $("#value2").attr("cityCode",cityCode)
        $("#value2").attr("countyCode",countyCode)
    });
    getAddrMess();
    /*获取焦点*/
    $("input").focus(function(){
        $(this).parents("li").next("li").addClass("hide");
    });
})
/*编辑地址*/
function getAddrMess(){
    if(receivingId==undefined||receivingId==null||receivingId==""){

    }else{
        ym.ajax({
            url : "/shop/address/getAddress",
            type : "post",
            param:{
                "receivingId":receivingId,
                ymUser:userMess,
            },
            success : function(data) {
                console.log(data);
                obj=data.obj;
                $("#name").val(data.obj.receiverName);
                $("#tel").val(data.obj.receiverPhone);
                $("#address").val(data.obj.detailedAddress);
                $("#value2").attr("provinceCode",data.obj.receiverState);
                $("#value2").attr("cityCode",data.obj.receiverCity);
                $("#value2").attr("countyCode",data.obj.receiverDistrict);
                $("#addrs").val(data.obj.receiverAddress);
            }
        })
    }
}
function addAddress(){
    var name=$("#name").val().trim();
    var tel=$("#tel").val().trim();
    var addrId=$("#value2").attr("provinceCode");
    var addr=$("#address").val();
    var addrs=$("#addrs").val();
    var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
    if(name.length<2){
        $(".nameTips").removeClass("hide");
    }else if(tel==""||!myreg.test(tel)){
        $(".telTips").removeClass("hide");
    }else if(addrId==""||addrId==undefined||addr==""){
        $(".addrTips").removeClass("hide");
    }else if(addrs==""){
        $(".addrsTips").removeClass("hide");
    }else{
       var data={};
       data.receiverName=name;
       data.receiverPhone=tel;
       data.receiverState=$("#value2").attr("provinceCode");
       data.receiverCity=$("#value2").attr("cityCode");
       data.receiverDistrict=$("#value2").attr("countyCode");
       data.receiverAddress=addrs;
       data.detailedAddress=$("#address").val();
       if(receivingId==undefined||receivingId==null||receivingId==""){
           //新增
           ym.ajax({
               url : "/shop/address/add",
               type : "post",
               param:{
                   "address":JSON.stringify(data),
                   ymUser:userMess,
               },
               success : function(data) {
                   if(data.res==1){
                       $("#alertModal .am-modal-bd").html("新增成功！")
                       $("#alertModal").modal();
                       setTime();
                   }
               }
           })
       }else{
           //编辑
           ym.ajax({
               url : "/shop/address/update",
               type : "post",
               param:{
                   receivingId:receivingId,
                   receiverName:name,
                   receiverPhone:tel,
                   receiverState:$("#value2").attr("provinceCode"),
                   receiverCity:$("#value2").attr("cityCode"),
                   receiverDistrict:$("#value2").attr("countyCode"),
                   receiverAddress:addrs,
                   detailedAddress:$("#address").val(),
               },
               success : function(data) {
                   if(data.res==1){
                       $("#alertModal .am-modal-bd").html("修改成功！")
                       $("#alertModal").modal();
                       setTime();
                   }
               }
           })
       }

    }
}
function setTime(){
    setTimeout(function () {
        $("#alertModal").modal('close');
        window.location.href="address.html?pageTips="+pageTips;
    }, 2000);
}


